package com.smart.mattress.account.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.mongodb.WriteResult;
import com.smart.mattress.account.dao.UserInfoDao;


/**
 * 用户信息数据库操作
 * Created by rencong on 17/3/10.
 */
@Repository
public class UserInfoDaoImpl implements UserInfoDao {

    Logger logger = LoggerFactory.getLogger(UserInfoDaoImpl.class);

    final String COLLECTION = "UserInfo";

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public boolean add(Object userInfo,String collectionName) {

        logger.info("userinfo:{}",userInfo);

        try{
            mongoTemplate.insert(userInfo,collectionName);
            return true;
        }catch(Exception e){
            logger.error("insert userinfo into db error:",e);
            return false;
        }
    }

    @Override
    public boolean delete(Query query,Class infoClass,String name) {
    	 try{
             mongoTemplate.remove(query, infoClass, name);
             return true;
         }catch(Exception e){
             logger.error("insert userinfo into db error:",e);
             return false;
         }
    }

    @Override
    public boolean update(Object userInfo,String collectionName) {
        try {
            mongoTemplate.save(userInfo, collectionName);
            return true;
        } catch (Exception e) {
            logger.error("update userinfo error:", e);
            return false;
        }
    }
    
    @Override
    public boolean update(Query query,Update update,Class o_class,String name){
    	try {
    		WriteResult s = mongoTemplate.updateFirst(query, update, o_class,name);
            return true;
        } catch (Exception e) {
            logger.error("update userinfo error:", e);
            return false;
        }
    }
    
    
    @Override
    public boolean update(Object concernInfo,Object userInfo1,Object userInfo2){
    	try {
            mongoTemplate.save(userInfo1,"UserInfo");
            mongoTemplate.save(userInfo2,"UserInfo");
            mongoTemplate.save(concernInfo,"ConcernInfo");
            return true;
        } catch (Exception e) {
            logger.error("update userinfo error:", e);
            return false;
        }
    }

    @Override
    public List findListByQuery(Query query,Class infoClass,String collectionName) {
        try{
            return mongoTemplate.find(query,infoClass,collectionName);
        }catch (Exception e){
            logger.error("find userinfo list error:",e);
            return null;
        }
    }

    @Override
    public Object findOneByQuery(Query query,Class infoClass,String collectionName) {
        try{
            return mongoTemplate.findOne(query,infoClass,collectionName);
        }catch (Exception e){
            logger.error("find one userinfo list error:",e);
            return null;
        }
    }

    @Override
    public long count(Query query,Class infoClass,String collectionName) {

        logger.info("count query:{}",query);

        try{
            return mongoTemplate.count(query,infoClass,collectionName);
        }catch (Exception e){
            logger.error("count error:",e);
            return 0l;
        }
    }
    
    @Override
    public boolean exist(Query query,Class infoClass){
    	try{
            return mongoTemplate.exists(query, infoClass);
        }catch (Exception e){
            logger.error("count error:",e);
            return false;
        }
    }
    
    @Override
    public boolean remove(Query query,Class infoClass,String name){
    	try{
    		mongoTemplate.remove(query, infoClass,name);
            return true;
        }catch (Exception e){
            logger.error("count error:",e);
            return false;
        }
    }
    
    

	@Override
	public boolean delete(String no) {
		// TODO Auto-generated method stub
		return false;
	}
}
